//
//  WaitLoadView.swift
//  CompareBuying
//
//  Created by 唐先才 on 16/2/26.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import UIKit

class WaitLoadView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        createWaitLoadView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func createWaitLoadView() {
        let waitView = UIView(frame: self.frame)
        waitView.backgroundColor = UIColor.whiteColor()
        self.addSubview(waitView)
        
        let activityId = UIActivityIndicatorView(activityIndicatorStyle: .White)
        activityId.color = MAIN_COLOR
        activityId .startAnimating()
        activityId.center = CGPointMake(self.frame.width / 2, HEIGHT / 2  - 40)
        waitView.addSubview(activityId)
        
        let lab = UILabel(frame: CGRect(x: 0, y: self.frame.height / 2 - 20, width: self.frame.width, height: 30))
        lab.text = "正在加载,请稍后"
        lab.textColor = MAIN_COLOR
        lab.font = UIFont.systemFontOfSize(15)
        lab.textAlignment = NSTextAlignment.Center
        waitView.addSubview(lab)
        
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(30 * Double(NSEC_PER_SEC)))
        dispatch_after(time, dispatch_get_main_queue(), { () -> Void in
            waitView.removeFromSuperview()
//            let alertView = UIAlertController(title: "提示", message: "网络连接失败", preferredStyle: .Alert)
//            alertView.addAction(UIAlertAction(title: "确定", style: .Default, handler: { (UIAlertAction) -> Void in
//                
//            }))
            let whiteView = UIView(frame: UIScreen.mainScreen().bounds)
            whiteView.backgroundColor = UIColor.whiteColor()
            self.addSubview(whiteView)
            let alertView = AlertViewController()
            whiteView.addSubview(alertView.view)
        })
    }
}
